package posfixform;

## This file is part of the Simplemail project.
#  Copyright (c) 2008 the Simplemail Team (see authors).
# 
#  For the full copyright and license information, please view the COPYING
#  file that was distributed with this source code. You can also view the
#  COPYING file online at http://unixforum.org.ua/misc/COPYING.txt
# 
#  @copyright  (c) the authors
#  @author     Dmitriy Shilenko <rtyug@ukr.net>
#  @license    GPLv2 http://unixforum.org.ua/misc/COPYING.txt
#  @package    Panel Mailbox
#  @version    $Id: $

use strict;
use CGI::Carp qw(fatalsToBrowser);
use base 'func';

sub new {
    my $self = shift->SUPER::new(@_);
    $self->dbi_connect if $self->can("dbi_connect");
    return $self;
}

sub select_all_posfixform {
    my $self = shift;

    my $c;
    my $sqlin0;
    my $sqlin1;
    if ( $self->{session}->{name} ne 'Admin' ) {
        $c      = '4';
        $sqlin0 = "
                          ,
                        t2.idname,
                        t2.m_domain,  
                        t2.m_alias,
                        t2.m_mailbox,
                        t2.mb_domain,
                        t2.mb_mailbox

                        FROM users  AS t1
                        LEFT JOIN admin_domain AS t2
                        ON t1.id = t2.idname
                        WHERE t1.id = ?
                        LIMIT 1  
 
 ";
    }
    else {
        $c      = '3';
        $sqlin1 = "
 
                     (SELECT COUNT(*)
                              FROM domain AS t1
                             ) AS c_domain_all,
                               
                              (SELECT COUNT(*)  
                         FROM domain AS t1,
                              mailbox AS t2
                         WHERE t1.domain = t2.domain) AS c_mailbox_all,

                             (SELECT COUNT(*)
                         FROM domain AS t1,
                              alias AS t2
                         WHERE t1.domain = t2.domain) AS c_alias_all, 
 
 ";
    }
    my $i   = '0';
    my @sql = ();
    for ( $i = 0 ; $i < $c ; $i++ ) {
        $sql[$i] = $self->{session}->{user};
    }

    my $sth = $self->{dbh}->prepare(
        "SELECT 
                            $sqlin1


                             (SELECT COUNT(*)
                              FROM domain AS t1
                               WHERE t1.idnane = ?) AS c_domain,
                               
                              (SELECT COUNT(*)  
                         FROM domain AS t1,
                              mailbox AS t2
                         WHERE t1.idnane = ? 
                         AND t1.domain = t2.domain) AS c_mailbox,

                             (SELECT COUNT(*)
                         FROM domain AS t1,
                              alias AS t2
                         WHERE t1.idnane = ? 
                         AND t1.domain = t2.domain ) AS c_alias
                               
                          $sqlin0
                         
                         "
    );
    $sth->execute(@sql);
    my $dd = $sth->fetchrow_hashref();
    $sth->finish();

    while ( my ( $key, $value ) = each( %{$dd} ) ) {

        if ( $key eq 'idname' && $value eq '' ) { $value = 'NO'; }
        if ( $key eq 'idname' && $value > 0 ) { $value = 'YES'; }

        if ( ( $key eq 'mb_domain' || $key eq 'mb_mailbox' ) && $value > 0 ) {
            $value = $value / 1048576;
            $value = int($value);
        }

        if ( $value eq '' ) { $value = '-'; }
        if (   $value == 0
            && $key ne 'c_domain'
            && $key ne 'c_mailbox'
            && $key ne 'c_alias'
            && $key ne 'c_domain_all'
            && $key ne 'c_mailbox_all'
            && $key ne 'c_alias_all' )
        {
            $value = 'Unlimited';
        }
        if ( $value == -1 ) { $value = 'Disable, no created/edit/delete'; }

        $self->{he}->{$key} = $value;
    }

    if ( $c == 4 ) {
        $self->{he}->{USER} = 1;
    }
    if ( $c == 3 ) {
        $self->{he}->{ADMIN} = 1;
    }

    return $self;
}

sub templ_hash_posfixform {
    my $self = shift;

    $self->{session}->{filet} = 'form';

    $self->SUPER::templ_hash(@_);
}

sub DESTROY {
    my $self = shift;
    $self->SUPER::DESTROY if $self->can("SUPER::DESTROY");
}

1;
